﻿using MediatR;
using SystemHYS.API.Application.command.ProductionRequisitionCommand;
using SystemHYS.Domian;
using SystemHYS.Infrstructre.Interface;
using SystemHYS.Infrstructre;
using SystemHYS.Domian.DTO;
namespace SystemHYS.API.Application.commandler.ProductionRequisitionCommandler
{
    public class ProductionRequisitionShowCommandler : IRequestHandler<ProductionRequisitionShowCommand, List<ProductionRequisitionDTO>>
    {
        IRawMaterialProductionRepository rawMaterialProductionRepository;
        IProductionRequisitionRepository productionRequisitionRepository;
        public SystemCoretext db;
        public ProductionRequisitionShowCommandler(IRawMaterialProductionRepository rawMaterialProductionRepository, IProductionRequisitionRepository productionRequisitionRepository, SystemCoretext db)
        {
            this.rawMaterialProductionRepository = rawMaterialProductionRepository;
            this.productionRequisitionRepository = productionRequisitionRepository;
            this.db = db;
        }
        public async Task<List<ProductionRequisitionDTO>> Handle(ProductionRequisitionShowCommand request, CancellationToken cancellationToken)
        {
            var num = 0;
            var plist = (from a in await productionRequisitionRepository.GetAllAsnc()
                        join b in await rawMaterialProductionRepository.GetAllAsnc() on a.productionRequisitionId equals b.productionRequisitionId
                        where a.Isdel == false
                        select b).ToList();
            foreach (var p in plist)
            {
                num += p.quantityReceived;
            }
            var list=(from a in await productionRequisitionRepository.GetAllAsnc() where a.Isdel==false select new ProductionRequisitionDTO
            {
                productionRequisitionId=a.productionRequisitionId,
                requisitionNumber=a.requisitionNumber,
                requisitionName=a.requisitionName,
                requisitionDate=a.requisitionDate,
                requisitionPerson = a.requisitionPerson,
                workOrderName = a.workOrderName,
                workOrderOrder= a.workOrderOrder,
                taskName= a.taskName,
                taskOrder = a.taskOrder,
                processRoute= a.processRoute,
                processName= a.processName,
                remark=a.remark,
                state=a.state,
                quantityReceived= num
            }).ToList();
            if (request.requisitionNumber!=null)
            {
                list=list.Where(x=>x.requisitionNumber.Contains(request.requisitionNumber)).ToList();
            }
            if (request.requisitionDate != null)
            {
                list = list.Where(x => x.requisitionDate==request.requisitionDate).ToList();
            }
            if (request.state != null)
            {
                list = list.Where(x => x.state==request.state).ToList();
            }
            return list.ToList();
        }
    }
}
